package com.yt.chapter1.test;

import java.util.*;

public class GetCenterPointFromListOfCoordinates {

    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 计算两个经纬度之间的距离
     *
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return
     */
    public static double GetDistance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.abs(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 1000);
        return s;
    }


    /**
     * 根据输入的地点坐标计算中心点
     *
     * @param geoCoordinateList
     * @return
     */
    public static GeoCoordinate getCenterPoint(List<GeoCoordinate> geoCoordinateList) {
        int total = geoCoordinateList.size();
        double X = 0, Y = 0, Z = 0;
        for (GeoCoordinate g : geoCoordinateList) {
            double lat, lon, x, y, z;
            lat = g.getLatitude() * Math.PI / 180;
            lon = g.getLongitude() * Math.PI / 180;
            x = Math.cos(lat) * Math.cos(lon);
            y = Math.cos(lat) * Math.sin(lon);
            z = Math.sin(lat);
            X += x;
            Y += y;
            Z += z;
        }

        X = X / total;
        Y = Y / total;
        Z = Z / total;
        double Lon = Math.atan2(Y, X);
        double Hyp = Math.sqrt(X * X + Y * Y);
        double Lat = Math.atan2(Z, Hyp);
        return new GeoCoordinate(Lat * 180 / Math.PI, Lon * 180 / Math.PI);
    }

    /**
     * 根据输入的地点坐标计算中心点（适用于400km以下的场合）
     *
     * @param geoCoordinateList
     * @return
     */
    public static GeoCoordinate getCenterPoint400(List<GeoCoordinate> geoCoordinateList) {
        // 以下为简化方法（400km以内）
        int total = geoCoordinateList.size();
        double lat = 0, lon = 0;
        for (GeoCoordinate g : geoCoordinateList) {
            lat += g.getLatitude() * Math.PI / 180;
            lon += g.getLongitude() * Math.PI / 180;
        }
        lat /= total;
        lon /= total;
        return new GeoCoordinate(lat * 180 / Math.PI, lon * 180 / Math.PI);
    }
    private static List<positionZoom> listPZ =null;
    static void InitPositionZoom() {
       listPZ = new ArrayList<>();
        positionZoom model = new positionZoom();
        //m
        model.setMixDis(0);
        model.setMaxDis(10);
        model.setZoom(19);
        listPZ.add(model);

        model = new positionZoom();
        model.setMixDis(10);
        model.setMaxDis(25);
        model.setZoom(18);
        listPZ.add(model);

        model = new positionZoom();
        model.setMixDis(25);
        model.setMaxDis(50);
        model.setZoom(17);
        listPZ.add(model);

        model = new positionZoom();
        model.setMixDis(50);
        model.setMaxDis(100);
        model.setZoom(16);
        listPZ.add(model);
        model = new positionZoom();
        model.setMixDis(100);
        model.setMaxDis(200);
        model.setZoom(15);
        listPZ.add(model);

        model = new positionZoom();
        model.setMixDis(200);
        model.setMaxDis(500);
        model.setZoom(14);
        listPZ.add(model);

        model = new positionZoom();
        model.setMixDis(500);
        model.setMaxDis(1000);
        model.setZoom(13);
        listPZ.add(model);


        model = new positionZoom();
        model.setMixDis(1000);
        model.setMaxDis(2000);
        model.setZoom(12);
        listPZ.add(model);

        model = new positionZoom();
        model.setMixDis(2000);
        model.setMaxDis(5000);
        model.setZoom(11);
        listPZ.add(model);

        model = new positionZoom();
        model.setMixDis(5000);
        model.setMaxDis(10000);
        model.setZoom(10);
        listPZ.add(model);

        model = new positionZoom();
        model.setMixDis(10000);
        model.setMaxDis(20000);
        model.setZoom(9);
        listPZ.add(model);


        model = new positionZoom();
        model.setMixDis(20000);
        model.setMaxDis(30000);
        model.setZoom(8);
        listPZ.add(model);

        model = new positionZoom();
        model.setMixDis(30000);
        model.setMaxDis(50000);
        model.setZoom(7);
        listPZ.add(model);

        model = new positionZoom();
        model.setMixDis(50000);
        model.setMaxDis(100000);
        model.setZoom(6);
        listPZ.add(model);

        model = new positionZoom();
        model.setMixDis(100000);
        model.setMaxDis(200000);
        model.setZoom(5);
        listPZ.add(model);

        model = new positionZoom();
        model.setMixDis(200000);
        model.setMaxDis(500000);
        model.setZoom(4);
        listPZ.add(model);


        model = new positionZoom();
        model.setMixDis(500000);
        model.setMaxDis(1000000);
        model.setZoom(3);
        listPZ.add(model);

    }

    public static void main(String[] args) {
        InitPositionZoom();
        List<GeoCoordinate> geoCoordinateList = new ArrayList<GeoCoordinate>();
        GeoCoordinate g = new GeoCoordinate();
        g.setLongitude(112.977324);
        g.setLatitude(28.178376);
        GeoCoordinate g2 = new GeoCoordinate();
        g2.setLongitude(112.975782);
        g2.setLatitude(28.172258);
        GeoCoordinate g3 = new GeoCoordinate();
        g3.setLongitude(113.025647);
        g3.setLatitude(28.193815);
        GeoCoordinate g4 = new GeoCoordinate();
        g4.setLongitude(113.145466);
        g4.setLatitude(28.302993);
        geoCoordinateList.add(g);
        geoCoordinateList.add(g2);
        geoCoordinateList.add(g3);
        geoCoordinateList.add(g4);
        GeoCoordinate re = GetCenterPointFromListOfCoordinates.getCenterPoint(geoCoordinateList);


        System.out.println("中心坐标为:"+re.getLongitude() + "," + re.getLatitude());
        double maxDis = 0;
        String str="";
        for (GeoCoordinate gc : geoCoordinateList) {

            double dis = GetDistance(re.getLatitude(), re.getLongitude(), gc.getLatitude(), gc.getLongitude());
            System.out.println(dis);
            if (dis > maxDis) {
                maxDis = dis;
                str=gc.getLatitude()+ "," +  gc.getLongitude();
            }
        }
        System.out.println("最长距离为："+maxDis+" 坐标为:"+str);

        int zoom=0;
        for (positionZoom pz : listPZ) {
            if (pz.getMixDis()<maxDis&&maxDis<=pz.getMaxDis()){
                zoom=pz.getZoom();
                break;
            }

        }

        System.out.println("地图缩放级别为："+zoom);
    }


}


class GeoCoordinate {
    private double latitude;
    private double longitude;

    public GeoCoordinate() {
    }

    public GeoCoordinate(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }


}




